package course.d1228;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
    访问static修饰的共享数据
 */
public class p4 {
    public static void main(String[] args) {

        Thread thread1 = new Thread(new Sale(), "A  ");
        Thread thread2 = new Thread(new Sale(), "B  ");
        Thread thread3 = new Thread(new Sale(), "C  ");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Sale implements Runnable {
    private static int TicketNum = 1000;
    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (TicketNum > 0) {
            lock.lock();
            try {
                if (TicketNum > 0) {
                    System.out.println(Thread.currentThread().getName() + " 正在卖第 " + TicketNum + " 张票，还有 " + --TicketNum + " 张票");
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
